﻿using Mozhi.Abp.Domain.BasicData;
using NPOI.HSSF.Record.Chart;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace mozhi.smarterp.Business.Products
{
    public class ProductPurchaseSetting : AuditedAggregateRoot<Guid>, IMultiTenant
    {
        public Guid? TenantId { get; private set; }

        public Guid ProductId { get; private set; }

        public Product Product { get; private set; }

        /// <summary>
        /// 采购默认单位
        /// </summary>
        public Guid? PurchaseUnitId { get; private set; }

        public Unit PurchaseUnit { get; private set; }

        public ProductAuxiliaryUnit ProductAuxiliaryUnit { get; private set; }

        /// <summary>
        /// 默认供应商
        /// </summary>
        public Guid? DefaultSupplierId { get; private set; }

        public Supplier DefaultSupplier { get; private set; }

        /// <summary>
        /// 默认采购员
        /// </summary>
        public Guid DefaultPurchaserId { get; private set; }

        public Employee DefaultPurchaser { get; private set; }

        /// <summary>
        /// 最小订购数量（基于基本单位）
        /// </summary>
        public decimal? MinPurchaseQuantity { get; private set; }

        private ProductPurchaseSetting() { }

        public ProductPurchaseSetting(Guid id, Product product, Unit purchaseUnit, ProductAuxiliaryUnit auxiliaryUnit, Supplier defaultSupplier, Employee defaultPurchaser, decimal? minPurchaseQuantity)
        {
            Product = product;
            DefaultSupplier = defaultSupplier;
            DefaultPurchaser = defaultPurchaser;
            PurchaseUnit = purchaseUnit;
            ProductAuxiliaryUnit = auxiliaryUnit;

            Id = id;
            ProductId = Product == null ? default : Product.Id;
            PurchaseUnitId = PurchaseUnit == null ? default : PurchaseUnit.Id;
            DefaultSupplierId = DefaultSupplier == null ? null : DefaultSupplier.Id;
            DefaultPurchaserId = DefaultPurchaser == null ? default : DefaultPurchaser.Id;
            MinPurchaseQuantity = minPurchaseQuantity;

            Validate();
        }

        public void Change(Product product, Unit purchaseUnit, ProductAuxiliaryUnit auxiliaryUnit, Supplier defaultSupplier, Employee defaultPurchaser, decimal? minPurchaseQuantity)
        {
            Product = product;
            DefaultSupplier = defaultSupplier;
            DefaultPurchaser = defaultPurchaser;
            PurchaseUnit = purchaseUnit;
            ProductAuxiliaryUnit = auxiliaryUnit;

            PurchaseUnitId = PurchaseUnit == null ? default : PurchaseUnit.Id;
            DefaultSupplierId = DefaultSupplier == null ? null : DefaultSupplier.Id;
            DefaultPurchaserId = DefaultPurchaser == null ? default : DefaultPurchaser.Id;
            MinPurchaseQuantity = minPurchaseQuantity;

            Validate();
        }

        #region
        private void Validate()
        {
            if (DefaultSupplier == null)
                throw new UserFriendlyException("供应商不存在", "100000");
            DefaultSupplier.Validate();

            if (DefaultPurchaser == null)
                throw new UserFriendlyException("采购员不存在", "100000");

            DefaultPurchaser.Validate();

            if (Product == null)
                throw new UserFriendlyException("产品不存在", "100000");

            Product.Validate();

            if (PurchaseUnit == null)
                throw new UserFriendlyException("采购单位不存在", "100000");

            PurchaseUnit.Validate();

            if (PurchaseUnit.Id != Product.BasicUnitId && ProductAuxiliaryUnit == null)
                throw new UserFriendlyException($"产品:{Product.Code}辅助单位：{PurchaseUnit.Name}换算关系不存在", "100000");

            if (ProductAuxiliaryUnit != null)
                ProductAuxiliaryUnit.Validate();

            Check.NotDefaultOrNull<Guid>(ProductId , nameof(ProductId));

        }
        #endregion
    }
}
